﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FlyItemPool
{
    public const float RecycleMaxTime = 8f;
    public static FlyItemPool Instance = new FlyItemPool();

    public GameObject m_BulletPoolObj;
    Dictionary<int, Dictionary<int, Queue<FlyItemObject>>> m_Pool = new Dictionary<int, Dictionary<int, Queue<FlyItemObject>>>();

    public void InitWorld(Scene scene, LoadSceneMode mode)
    {
        InitWorld();
    }
    public void InitSystem()
    {
        //  因为加载的第一个场景InitWorld不会被调用，所以这里手动调用一下
        // becase the function will do not be called at the first scene , so i call the InitWorld Function Manually
        InitWorld();
    }
    void InitWorld()
    {
        m_BulletPoolObj = new GameObject();
        m_BulletPoolObj.name = "BulletPools";
        m_Pool.Clear();
    }

    public bool Recycle(FlyItemObject flyItem)
    {
        if (flyItem == null)
        {
            MessageShow.Instance.Warning("you are trying push a empty reference into pool of flyItem");
            return false;
        }
        flyItem.isRecycled = true;
        flyItem.RemoveIgnoreCollision();
        flyItem.gameObject.SetActive(false);
        if (!m_Pool.ContainsKey(flyItem.weapon.typeID))
        {
            m_Pool.Add(flyItem.weapon.typeID, new Dictionary<int, Queue<FlyItemObject>>());
        }
        if (!m_Pool[flyItem.weapon.typeID].ContainsKey(flyItem.flyItemID))
        {
            m_Pool[flyItem.weapon.typeID].Add(flyItem.flyItemID, new Queue<FlyItemObject>());
        }
        m_Pool[flyItem.weapon.typeID][flyItem.flyItemID].Enqueue(flyItem);

        return true;
        //FlyItem bullet = bulletObject.GetComponent<FlyItem>();
        //if (null == bullet || bullet.WeaponClassName == "")
        //    return false;


        //if (!m_Pool.ContainsKey(bullet.WeaponClassName))
        //    m_Pool.Add(bullet.WeaponClassName, new List<GameObject>());

        //m_Pool[bullet.WeaponClassName].Add(bulletObject);
        //bullet.Death = true;
        //bulletObject.transform.position = new Vector3(0, 100000000, 0);
        //if(NetworkServer.active)
        //{
        //    bullet.RpcOnDeath();
        //}
        ////if (bulletObject.activeSelf == true)
        ////    bulletObject.SetActive(false);
    }

    public FlyItemObject Take(Weapon weapon, int flyItemID)
    {
        if(!m_Pool.ContainsKey(weapon.typeID) || 
            !m_Pool[weapon.typeID].ContainsKey(flyItemID) || 
            m_Pool[weapon.typeID][flyItemID].Count <= 0)
        {
            var flyItemObject = FlyItemObject.Create(weapon, flyItemID);
            flyItemObject.gameObject.transform.SetParent(m_BulletPoolObj.transform);
            flyItemObject.CountStart();
            if (flyItemObject.GetComponent<Collider2D>())
                flyItemObject.GetComponent<Collider2D>().isTrigger = false;
            return flyItemObject;
        }
        var flyItem = m_Pool[weapon.typeID][flyItemID].Dequeue(); 
        flyItem.gameObject.SetActive(true);
        flyItem.isRecycled = false;
        flyItem.CountStart();
        if (flyItem.GetComponent<Collider2D>())
            flyItem.GetComponent<Collider2D>().isTrigger = false;
        return flyItem;


        //string weaponType = weapon.m_Type.ToString();

        //if (m_Pool.ContainsKey(weaponType))
        //{
        //    var bulletPool = m_Pool[weaponType];
        //    if (bulletPool.Count > 0)
        //    {
        //        var retValue = bulletPool[bulletPool.Count - 1];
        //        bulletPool.RemoveAt(bulletPool .Count - 1);
        //        retValue.GetComponent<FlyItem>().Death = false;
        //        return retValue;
        //    }
        //}

        //UnityEngine.Object obj = Resources.Load<GameObject>(weapon.BulletResource);
        //if (null == obj)
        //{
        //    Debug.LogError("Bullet is null. not found bullet resouce in " + weapon.BulletResource);
        //    return null;
        //}
        //var bullet = UnityEngine.Object.Instantiate(obj) as GameObject;
        //var bulletComponent = bullet.GetComponent<FlyItem>();
        //bulletComponent.Death = false;
        //bullet.transform.SetParent(m_BulletPoolObj.transform);
        ////if(bullet.activeSelf == false)
        ////    bullet.SetActive(true);
        //return bullet;
    }
}